<?php
//后台员工管理

define('IN_MMOA', true);

require dirname(__FILE__).'/includes/init.php';


if ($_REQUEST['act'] == '')
{
	$sql = "SELECT * FROM " . $mmoa->table('employees') . " WHERE 1";
	$employees = $db->getAll($sql);
	
	for ($i=0; $i<count($employees); $i++){
		$departid = $employees[$i]['e_depart'];
		$table = $mmoa->table('departments');
		$departname = $db->getOne("SELECT d_name FROM $table WHERE id=$departid ");
		if (!empty($departname)){
			$employees[$i]['e_depart'] = $departname;
		}
		
	}
	
	$smarty->assign('employees', $employees);

	$smarty->assign('mm_show', 'e_list');

	clear_caches();
	$smarty->display('employee.htm');
}
elseif ($_REQUEST['act'] == 'add')
{
	// 添加员工信息
	//echo 'ok';
	
	$d = department_show();
	$smarty->assign('depart', $d);
	
	$g = group_show();
	$smarty->assign('group', $g);
	
	$smarty->assign('mm_show', 'e_add');
	
	clear_caches();
	$smarty->display('employee.htm');
}
elseif ($_REQUEST['act'] == 'addu')
{
	// 插入员工信息
	$e = array();
	// 基本信息
	$e['name'] = isset($_POST['name']) ? trim($_POST['name']) : '';
	$e['pass'] = isset($_POST['pass']) ? trim($_POST['pass']) : md5('888');
	$e['sex'] = isset($_POST['sex']) ? trim($_POST['sex']) : '0';
	$e['depart'] = isset($_POST['depart']) ? trim($_POST['depart']) : '';
	$e['group'] = isset($_POST['group']) ? trim($_POST['group']) : '正式员工';
	// 更多信息
	$e['admin'] = isset($_POST['admin']) ? trim($_POST['admin']) : '';
	$e['college'] = isset($_POST['college']) ? trim($_POST['college']) : '';
	$e['cardid'] = isset($_POST['cardid']) ? trim($_POST['cardid']) : '';
	$e['homeaddress'] = isset($_POST['homeaddress']) ? trim($_POST['homeaddress']) : '';
	$e['address'] = isset($_POST['address']) ? trim($_POST['address']) : '';
	$e['phone'] = isset($_POST['phone']) ? trim($_POST['phone']) : '';
	$e['econtact'] = isset($_POST['econtact']) ? trim($_POST['econtact']) : '';
	$e['ephone'] = isset($_POST['ephone']) ? trim($_POST['ephone']) : '';
	
	// 更新表
	if (insert_e($e)){
		echo '<meta charset="utf-8">';
		echo '添加成功';
	} else {
		echo '<meta charset="utf-8">';
		echo '添加失败';
	}
	
}
elseif ($_REQUEST['act'] == 'mod')
{
	// 修改员工信息
	//echo 'ok';
	$d = department_show();
	$smarty->assign('depart', $d);
	
	$g = group_show();
	$smarty->assign('group', $g);
	
	$eid=$_GET['eid'];
	$sql = "SELECT * FROM " . $mmoa->table('employees') . " WHERE id = $eid";
	$e = $db->getRow($sql);
	
	
	$smarty->assign('employee', $e);
	$smarty->assign('eid', $eid);
	
	$smarty->assign('mm_show', 'e_mod');
	
	clear_caches();
	$smarty->display('employee.htm');
}
elseif ($_REQUEST['act'] == 'modu')
{
	// 更新员工表
	$e = array();
	// 基本信息
	$e['name'] = isset($_POST['name']) ? trim($_POST['name']) : '';
	//$e['pass'] = isset($_POST['pass']) ? md5($_POST['pass']) : md5('888');
	$e['sex'] = isset($_POST['sex']) ? trim($_POST['sex']) : '0';
	$e['depart'] = isset($_POST['depart']) ? trim($_POST['depart']) : '';
	$e['group'] = isset($_POST['group']) ? trim($_POST['group']) : '正式员工';
	// 更多信息
	$e['admin'] = isset($_POST['admin']) ? trim($_POST['admin']) : '';
	$e['college'] = isset($_POST['college']) ? trim($_POST['college']) : '';
	$e['cardid'] = isset($_POST['cardid']) ? trim($_POST['cardid']) : '';
	$e['homeaddress'] = isset($_POST['homeaddress']) ? trim($_POST['homeaddress']) : '';
	$e['address'] = isset($_POST['address']) ? trim($_POST['address']) : '';
	$e['phone'] = isset($_POST['phone']) ? trim($_POST['phone']) : '';
	$e['econtact'] = isset($_POST['econtact']) ? trim($_POST['econtact']) : '';
	$e['ephone'] = isset($_POST['ephone']) ? trim($_POST['ephone']) : '';	
	
	$eid = $_POST['eid'];
	
	// 默认密码不变
	$pass = isset($_POST['pass']) ? $_POST['pass'] :'';
	if (!empty($pass)){
	  $sqlp = " SELECT e_pass FROM " . $mmoa->table('employees') . " WHERE id = $eid";
	  $oldpass = $db->getOne($sqlp);
	  if ($oldpass == $pass){
	  	$e['pass'] = $oldpass;
	  } else {
	  	$e['pass'] = md5($pass);
	  }
	} else {
		$e['pass'] = md5('888');
	}

	// 更新表
	if (update_e($e, $eid)){
		echo '<meta charset="utf-8">';
		echo '更新成功';
	} else {
		echo '<meta charset="utf-8">';
		echo '更新失败';
	}

}
elseif ($_REQUEST['act'] == 'del')
{
	// 删除员工信息
	//echo 'ok';
	if ($_GET['eid']){
		$eid = $_GET['eid'];
		$sql = "DELETE FROM " . $mmoa->table('employees') . " WHERE id=$eid";
		if($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '删除成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '删除失败';
		}
	}
}


//-------- Functions
function insert_e($e){
	if ($e){
		$sql = "INSERT INTO " . $GLOBALS['mmoa']->table('employees') . "( `e_depart`, `e_name`, `e_pass`, `e_sex`, `e_college`, `e_card_id`, `e_home_address`, `e_address`, `e_phone`, `e_emerg_contact`, `e_emerg_phone`, `is_admin`, `e_group`) 
				VALUES ('".$e['depart']."',
						'".$e['name']."',
						'".$e['pass']."',
						'".$e['sex']."',
						'".$e['college']."',
						'".$e['cardid']."',
						'".$e['homeaddress']."',
						'".$e['address']."',
						'".$e['phone']."',
						'".$e['econtact']."',
						'".$e['ephone']."',
						'".$e['admin']."',
						'".$e['group']."')";
		$in = $GLOBALS['db']->query($sql);
		if ($in){
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function update_e($e, $eid){
	if ($e){
		$sql = "UPDATE " . $GLOBALS['mmoa']->table('employees') . " SET `e_depart` = '".$e['depart']."', 
				`e_name` = '".$e['name']."', 
				`e_pass` = '".$e['pass']."', 
				`e_sex` = '".$e['sex']."',
				`e_college` = '".$e['college']."', 
				`e_card_id` = '".$e['cardid']."', 
				`e_home_address` = '".$e['homeaddress']."', 
				`e_address` = '".$e['address']."', 
				`e_phone` = '".$e['phone']."', 
				`e_emerg_contact` = '".$e['econtact']."', 
				`e_emerg_phone` = '".$e['ephone']."', 
				`is_admin` = '".$e['admin']."', 
				`e_group` = '".$e['group']."'
				WHERE id = $eid";
		$in = $GLOBALS['db']->query($sql);
		if ($in){
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
?>